using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
    class MuaHangDAO : BaseDAO
    {
        // save into DB, @id = -1: insert else, update
        public ResultObj SaveMuaHang(MuaHang muahang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddMuaHang", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddMuaHang";
            SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, muahang.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, muahang.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, muahang.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, muahang.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, muahang.History);
            cmd.Parameters.Add(param);
            param = CreateParameter("@NguoiLap", DbType.String, ParameterDirection.Input, muahang.NguoiLap);
            cmd.Parameters.Add(param);
            param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, muahang.NgayThang);
            cmd.Parameters.Add(param);
            param = CreateParameter("@NhaCungCap", DbType.String, ParameterDirection.Input, muahang.NhaCungCap);
            cmd.Parameters.Add(param);
            param = CreateParameter("@DiaChi", DbType.String, ParameterDirection.Input, muahang.DiaChi);
            cmd.Parameters.Add(param);
            param = CreateParameter("@NguoiGiaoHang", DbType.String, ParameterDirection.Input, muahang.NguoiGiaoHang);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdChiNhanhCongTy", DbType.Int32, ParameterDirection.Input, muahang.IdChiNhanhCongTy);
            cmd.Parameters.Add(param);
            param = CreateParameter("@DienGiai", DbType.String, ParameterDirection.Input, muahang.DienGiai);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ChungTuKemTheo", DbType.String, ParameterDirection.Input, muahang.ChungTuKemTheo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@VAT", DbType.Int64, ParameterDirection.Input, muahang.VAT);
            cmd.Parameters.Add(param);
            param = CreateParameter("@TienVAT", DbType.Int64, ParameterDirection.Input, muahang.TienVAT);
            cmd.Parameters.Add(param);
            param = CreateParameter("@TongCong", DbType.Int64, ParameterDirection.Input, muahang.TongCong);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, muahang.IsDelete);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IsNhapKho", DbType.Int32, ParameterDirection.Input, muahang.IsNhapKho);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();
            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);

            return result;
        }

        // Get all MuaHang in DB with IsDelete = 0
        public ResultObj GetAllMuaHang()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<MuaHang> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllMuaHang", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<MuaHang>();
                MuaHang item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new MuaHang();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.NhaCungCap = Convert.ToString(reader["NhaCungCap"]);
                    item.DiaChi = Convert.ToString(reader["DiaChi"]);
                    item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.DienGiai = Convert.ToString(reader["DienGiai"]);
                    item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
                    item.VAT = Convert.ToInt64(reader["VAT"]);
                    item.TienVAT = Convert.ToInt64(reader["TienVAT"]);
                    item.TongCong = Convert.ToInt64(reader["TongCong"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    item.IsNhapKho = Convert.ToInt32(reader["IsNhapKho"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Get all MuaHang in DB with condition
        public ResultObj GetAllMuaHangByID(MuaHang muahang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<MuaHang> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllMuaHangByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, muahang.Id);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<MuaHang>();
                MuaHang item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new MuaHang();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.NhaCungCap = Convert.ToString(reader["NhaCungCap"]);
                    item.DiaChi = Convert.ToString(reader["DiaChi"]);
                    item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.DienGiai = Convert.ToString(reader["DienGiai"]);
                    item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
                    item.VAT = Convert.ToInt64(reader["VAT"]);
                    item.TienVAT = Convert.ToInt64(reader["TienVAT"]);
                    item.TongCong = Convert.ToInt64(reader["TongCong"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    item.IsNhapKho = Convert.ToInt32(reader["IsNhapKho"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        public ResultObj GetAllMuaHangByMaSo(MuaHang muahang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<MuaHang> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllMuaHangByMaSo", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, muahang.MaSo);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<MuaHang>();
                MuaHang item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new MuaHang();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.NhaCungCap = Convert.ToString(reader["NhaCungCap"]);
                    item.DiaChi = Convert.ToString(reader["DiaChi"]);
                    item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.DienGiai = Convert.ToString(reader["DienGiai"]);
                    item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
                    item.VAT = Convert.ToInt64(reader["VAT"]);
                    item.TienVAT = Convert.ToInt64(reader["TienVAT"]);
                    item.TongCong = Convert.ToInt64(reader["TongCong"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    item.IsNhapKho = Convert.ToInt32(reader["IsNhapKho"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Delete a MuaHang out of DB with condition
        public ResultObj DeleteMuaHang(MuaHang muahang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteMuaHang", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteMuaHang";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, muahang.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }

        // Get info of CTMuaHang from DB with condition
        public ResultObj GetCTMuaHang(int IdCTMuaHang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<CTMuaHang> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetCTMuaHangByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdCTMuaHang);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<CTMuaHang>();
                CTMuaHang item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new CTMuaHang();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdMuaHang = Convert.ToInt32(reader["IdMuaHang"]);
                    item.TenHang = Convert.ToString(reader["TenHang"]);
                    item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
                    item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
                    item.DonGia = Convert.ToInt64(reader["DonGia"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
    }
}
